/*****************************************************************************
 * plum_hal_uart.h
 *
 * Copyright (C) 2020 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

#ifndef __PLUM_HAL_UART_H__
#define __PLUM_HAL_UART_H__

/*****************************************************************************
 * Included Files
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Public Types
 ****************************************************************************/

typedef enum {
    PLUM_HAL_UART_STOPBIT_0_5,
    PLUM_HAL_UART_STOPBIT_1,
    PLUM_HAL_UART_STOPBIT_1_5,
    PLUM_HAL_UART_STOPBIT_2,
} plum_hal_uart_stopbit_t;

typedef enum {
    PLUM_HAL_UART_PARITY_NONE,
    PLUM_HAL_UART_PARITY_EVEN,
    PLUM_HAL_UART_PARITY_ODD,
} plum_hal_uart_parity_t;

typedef enum {
    PLUM_HAL_UART_DATABIT_5,
    PLUM_HAL_UART_DATABIT_6,
    PLUM_HAL_UART_DATABIT_7,
    PLUM_HAL_UART_DATABIT_8,
    PLUM_HAL_UART_DATABIT_9,
} plum_hal_uart_databit_t;

typedef enum {
    PLUM_HAL_UART_FLOW_CTRL_NONE,
    PLUM_HAL_UART_FLOW_CTRL_CTS,
    PLUM_HAL_UART_FLOW_CTRL_RTS,
    PLUM_HAL_UART_FLOW_CTRL_RTS_CTS,
} plum_hal_uart_flow_ctrl_t;

typedef struct {
    plum_u32 rx;
    plum_u32 tx;
} plum_hal_uart_gpio_t;

typedef struct {
    plum_hal_uart_databit_t   databit;
    plum_hal_uart_parity_t    parity;
    plum_hal_uart_stopbit_t   stopbit;
    plum_hal_uart_flow_ctrl_t flowctrl;
    plum_u32                  baudrate;
    plum_u32                  rxbuf_size;
    plum_hal_uart_gpio_t      gpio;
} plum_hal_uart_cof_t;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/*****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

/**
 * @brief:  串口初始化
 *
 * @param[in]:  id      -   plum串口ID，如PLUM_HAL_UART_ID(1)
 * @param[in]:  config  -   串口参数配置
 *
 * @return: 返回执行结果
 **/
plum_s32 plum_hal_uart_init(plum_u32 id, plum_hal_uart_cof_t *config);

/**
 * @brief:  串口发送数据
 *
 * @param[in]:  id      -   plum串口ID，如PLUM_HAL_UART_ID(1)
 * @param[in]:  data    -   待发送数据指针
 * @param[in]:  cnt     -   待发送数据长度
 *
 * @return: 大于0   -   成功接收发送的数据长度
 *          小于0   -   错误码
 **/
plum_s32 plum_hal_uart_send(plum_u32 id, PLUM_CONST plum_void *data,
                            plum_u16 cnt);

/**
 * @brief:  串口接收数据
 *
 * @param[in]:  id      -   plum串口ID，如PLUM_HAL_UART_ID(1)
 * @param[out]: data    -   接收数据存储指针
 * @param[in]:  size    -   最大接收长度
 * @param[in]:  timeout -   接收数据超时时间
 *
 * @return: 大于0   -   成功接收到的数据长度
 *          小于0   -   错误码
 **/
plum_s32 plum_hal_uart_recv(plum_u32 id, plum_void *data, plum_u16 size,
                            plum_u32 timeout);

/****************************************************************************/
#ifdef __cplusplus
}
#endif
/****************************************************************************/
#endif /* __PLUM_HAL_UART_H__ */
/****************************************************************************/

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
